//
//  CustomPhotoLiaarayViewController.m
//  自定义相册
//
//  Created by MyMac on 15/6/30.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//

#import "CustomPhotoLiaarayViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
@interface CustomPhotoLiaarayViewController ()
@property(nonatomic,strong)ALAssetsLibrary*assetsLibrary;
@property(nonatomic,strong)NSMutableArray*albumsArray;
@property(nonatomic,strong)NSMutableArray*imageAssetArray;
@end
//ALAssetsGroup照片库的某一个相册
//ALAseset某个照片或视频
//
@implementation CustomPhotoLiaarayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    ALAuthorizationStatus state=[ALAssetsLibrary authorizationStatus];
    if(state==ALAuthorizationStatusDenied||state==ALAuthorizationStatusRestricted){
        NSLog(@"请授权允许访问相册");
    
    }else{
        self.assetsLibrary=[[ALAssetsLibrary alloc]init];
        self.albumsArray=[[NSMutableArray alloc]init];
        self.imageAssetArray=[[NSMutableArray alloc]init];
        [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
            NSLog(@"相册线程*****%@",[NSThread currentThread]);
            static int i=0;
            i++;
            NSLog(@"%d",i);
            if(group){
                [group setAssetsFilter:[ALAssetsFilter allPhotos]];
                if(group.numberOfAssets>0){
                
                    [self.albumsArray addObject:group];
                }
            
            
            }else{
            
                [self imageSearch];
            
            }//异步遍历，，所以当group为nil时就遍历完了,返回数据时回到主线程，所以当前为主线程
            
        } failureBlock:^(NSError *error) {
            NSLog(@"失败");
        }];//遍历相册库列表，获取相册,
    }
}
-(void)imageSearch
{
    for(ALAssetsGroup*asseteGroup in self.albumsArray){
        [asseteGroup enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
               NSLog(@"照片线程*****%@",[NSThread currentThread]);
            NSLog(@"#####%lu",(long)index);
            if(result)
            [self.imageAssetArray addObject:result];
            else{
                UIScrollView*scrollview=[[UIScrollView alloc]initWithFrame:self.view.bounds];
                [self.view addSubview:scrollview];
                for (int i=0; i<self.imageAssetArray.count; i++) {
                    ALAsset*asset=self.imageAssetArray[i];
                    CGImageRef iam=asset.thumbnail;
                    //for(int j=0;j<5;j++){
                        CGFloat w=self.view.bounds.size.width/5.0;
                        CGFloat h=60;
                        CGFloat y=i/5*h;
                        CGFloat x=w*(i%5);
                        UIImageView*imageView=[[UIImageView alloc]initWithFrame:CGRectMake(x,y,w, h)];
                        imageView.image=[UIImage imageWithCGImage:iam];
                        [scrollview addSubview:imageView];
                        scrollview.contentSize=CGSizeMake(self.view.bounds.size.width, y+h);
                    
                    
                    
                    //}
                   
                }
            
            
            }//也是异步遍历，当result为nil时，遍历完毕
        }];
        
        
        
    }


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
